// hyb_yl/mysubpages/pages/docorder/docorder.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabitem: ['待接诊', '已接诊', '已结束', '已退回'],
    docindex: 0,
    orderlist: [{
        'state': 0,
      },
      {
        'state': 1,
      },
      {
        'state': 2,
      },
      {
        'state': 3,
      }
    ],
    ifpay: 1 //已支付待接诊
  },
  doctab: function (e) {
    var that = this
    const ifpay = e.currentTarget.dataset.dex;
    console.log(e)
    var key_words = that.data.key_words
    var zid = that.data.zid
    var id = that.data.id
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.dhorderlistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
    if (key_words == 'tuwenwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.doclistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
    //处方订单
    if (key_words == 'yuanchengkaifang') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.getcflistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
    this.setData({
      docindex: (ifpay - 1)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var state = options.state;
    var titlme = options.titlme
    var id = options.id
    var key_words = options.key_words
    var zid = options.zid
    var ifpay = that.data.ifpay
    var z_telephone = options.z_telephone
    that.setData({
      docindex: state,
      ftitle: titlme,
      id: id,
      key_words: key_words,
      z_telephone: z_telephone,
      zid: zid,
    })
    wx.setNavigationBarTitle({
      title: titlme + '订单'
    })
    //查询咨询我的订单
    if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.dhorderlistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
    if (key_words == 'tuwenwenzhen') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.doclistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
    //处方订单
    if (key_words == 'yuanchengkaifang') {
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.getcflistser',
        data: {
          zid: zid,
          key_words: key_words,
          id: id,
          ifpay: ifpay
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if(currPage.__data__.docindex =='undefined' || currPage.__data__.docindex ==undefined){
      var num = parseInt(0)
    }else{
      var num = parseInt(currPage.__data__.docindex)
    }
    var ifpay = (num + 1)
    var key_words = that.data.key_words
    var zid = that.data.zid
    var id = that.data.id
    console.log(num,ifpay, key_words, zid, id)
    if (currPage.data.docindex) {

      if (key_words == 'dianhuajizhen' || key_words == 'shipinwenzhen') {
        app.util.request({
          'url': 'entry/wxapp/zhuanjia.dhorderlistser',
          data: {
            zid: zid,
            key_words: key_words,
            id: id,
            ifpay: ifpay
          },
          success: function (res) {
            console.log(res)
            that.setData({
              list: res.data
            })
          }
        })
      }
      if (key_words == 'tuwenwenzhen') {
        app.util.request({
          'url': 'entry/wxapp/zhuanjia.doclistser',
          data: {
            zid: zid,
            key_words: key_words,
            id: id,
            ifpay: ifpay
          },
          success: function (res) {
            console.log(res)
            that.setData({
              list: res.data
            })
          }
        })
      }
      //处方订单
      if (key_words == 'yuanchengkaifang') {
        app.util.request({
          'url': 'entry/wxapp/zhuanjia.getcflistser',
          data: {
            zid: zid,
            key_words: key_words,
            id: id,
            ifpay: ifpay
          },
          success: function (res) {
            console.log(res)
            that.setData({
              list: res.data
            })
          }
        })
      }
      this.setData({
        docindex: currPage.data.docindex
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  getchar(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var zid = that.data.zid
    var typedate = e.currentTarget.dataset.typedate
    var key_words = that.data.key_words
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var openid = e.currentTarget.dataset.openid
    var overtime = e.currentTarget.dataset.overtime
    var ifpay = e.currentTarget.dataset.ifpay
    var docindex = that.data.docindex
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&typedate=' + typedate + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime + '&ifpay=' + ifpay + '&docindex=' + docindex,
    })
  },
  joinm(e) {
    console.log(e)
    var roomID = e.currentTarget.dataset.roomid
    var sdkAppID = e.currentTarget.dataset.sdkappid
    var template = e.currentTarget.dataset.template
    var userID = e.currentTarget.dataset.userid
    var id = e.currentTarget.dataset.id
    var userSig = e.currentTarget.dataset.usersig
    var orders = e.currentTarget.dataset.orders
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.updatesporder',
      data: {
        orders: orders
      },
      success: function (res) {
        console.log(res)
      }
    })
    var url = "/hyb_yl/webrtc-room/room/room?roomID=" + roomID + '&template=' + template + '&sdkAppID=' + sdkAppID + '&userId=' + userID + '&userSig=' + userSig + '&id=' + id + '&typeindex=1';
    wx.navigateTo({
      url: url,
    })
  },
  entrec(e) {
    var that = this
    var z_telephone = that.data.z_telephone
    var j_id = e.currentTarget.dataset.j_id
    var privateNum = e.currentTarget.dataset.private
    console.log(z_telephone, j_id, privateNum)
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = that.data.zid
    app.util.request({
      'url': 'entry/wxapp/yuyue.setaxndel',
      data: {
        doc_phone: z_telephone,
        back_orser: back_orser,
        privateNum: privateNum
      },
      success: function (res) {
        console.log(res)
        if (res.data.resultcode == '1016001') {
          wx.showToast({
            title: '您已拨打过电话',
          })
        } else {
          if (res.data.resultdesc == 'Success') {
            wx.makePhoneCall({
              phoneNumber: privateNum,
              success: function () {
                console.log("拨打电话成功！")
                app.util.request({
                  'url': 'entry/wxapp/zhuanjia.updatesporder',
                  data: {
                    orders: back_orser
                  },
                  success(res) {
                    console.log(res)
                  }
                })
              },
              fail: function () {
                console.log("拨打电话失败！")
              },
              complete: function () {
                //执行解绑
                // setTimeout(function () {
                //   app.util.request({
                //     'url': 'entry/wxapp/yuyue.uphuawtel',
                //     data: {
                //       doc_phone: z_telephone,
                //       j_id: j_id,
                //       privateNum: privateNum,
                //       back_orser: back_orser,
                //       zid: zid
                //     },
                //     success: function (res) {
                //       console.log(res)
                //       //成功后改变状态
                //     }
                //   })
                // }, 40000)
              }
            })
          }
        }

      }
    })
  },
  twentrec(e) {
    var that = this
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = that.data.zid
    var key_words = that.data.key_words
    var id = e.currentTarget.dataset.id
    var openid = e.currentTarget.dataset.openid
    wx.showModal({
      title: '提示',
      content: '是否接诊',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.updatetwstate',
            data: {
              back_orser: back_orser
            },
            success: function (res) {
              console.log(res)
              var overtime = res.data.overtime
              wx.navigateTo({
                url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime,
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  chufangentr(e) {
    var that = this
    var j_id = e.currentTarget.dataset.j_id
    var back_orser = e.currentTarget.dataset.back_orser
    var zid = that.data.zid
    var key_words = that.data.key_words
    var id = e.currentTarget.dataset.id
    var openid = e.currentTarget.dataset.openid
    wx.showModal({
      title: '提示',
      content: '是否接诊',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.updatecfstate',
            data: {
              back_orser: back_orser
            },
            success: function (res) {
              console.log(res)
              var overtime = res.data.overtime
              wx.navigateTo({
                url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiahuida/zhuanjiahuida?id=' + id + '&zid=' + zid + '&key_words=' + key_words + '&j_id=' + j_id + '&back_orser=' + back_orser + '&openid=' + openid + '&overtime=' + overtime,
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})